﻿using Models;
using Services.Model;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Services
{
    public class Taxi: ITaxi
    {
        private DatabaseContext db = new DatabaseContext();

        public Taxi()
        {
            Database.SetInitializer(new DatabaseInitializer());
        }

        public List<TaiXe> GetAllTaiXeIn5Km(double x, double y)
        {
            return db.TaiXe.ToList();
        }


        public List<NguoiDung> GetAllNguoiDung()
        {
            return db.NguoiDung.ToList();
        }

        public bool AddNguoiDung(NguoiDung nguoidung)
        {
            try
            {
                db.NguoiDung.Add(nguoidung);
                db.SaveChanges();
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        public bool UpdateNguoiDung(NguoiDung nguoidung)
        {
            try
            {
                var user = new NguoiDung();
                user = db.NguoiDung.SingleOrDefault(n => n.Id.Equals(nguoidung.Id));
                user.KinhDo = nguoidung.KinhDo;
                user.ViDo = nguoidung.ViDo;
                user.SoChuyen = nguoidung.SoChuyen;
                user.TongDoDai = nguoidung.TongDoDai;
                user.TongTien = nguoidung.TongTien;
                db.SaveChanges();
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        public NguoiDung GetNguoiDungByID(int id)
        {
            try
            {
                var user = new NguoiDung();
                user = db.NguoiDung.SingleOrDefault(n => n.Id.Equals(id));
                return user;
            }
            catch (Exception ex)
            {
                return null;
            }
        }

        public NguoiDung GetNguoiDungByMaMay(string mamay)
        {
            try
            {
                var user = new NguoiDung();
                user = db.NguoiDung.SingleOrDefault(n => n.MaMay.Equals(mamay));
                return user;
            }
            catch (Exception ex)
            {
                return null;
            }
        }

        public bool AddTaiXe(TaiXe taixe)
        {
            try
            {
                db.TaiXe.Add(taixe);
                db.SaveChanges();
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        public bool UpdateTaiXe(TaiXe taixe)
        {
            try
            {
                var currenttaixe = new TaiXe();
                currenttaixe = db.TaiXe.SingleOrDefault(t => t.Id.Equals(taixe.Id));
                currenttaixe.KinhDo = taixe.KinhDo;
                currenttaixe.ViDo = taixe.ViDo;
                currenttaixe.SoBinhChon = taixe.SoBinhChon;
                db.SaveChanges();
                return true;

            }
            catch (Exception ex)
            {
                return false;
            }
        }

        public List<TaiXe> GetTaiXeByLocation(double x, double y)
        {
            return db.TaiXe.Where(t => (Math.Abs(t.ViDo - x) * 111 <= 5 )&& (Math.Abs(t.KinhDo - y) * 111 <= 5)).ToList();
        }


        public List<TaiXe> GetAllTaiXe()
        {
            throw new NotImplementedException();
        }


    }
}
